package com.ymx.darling.util;

import java.util.LinkedList;
import java.util.List;

/**
 * 动态拼接字符串
 * 内部使用LinkedList的数据结构存放每次插入的数据
 *
 * @author 爱java的小于
 */
public class StringLink {
    private final List<String> linkList = new LinkedList<>();

    public StringLink(String s) {
        this.linkList.add(s);
    }

    public StringLink() {}

    @Override
    public String toString(){
        StringBuilder builder = new StringBuilder();
        linkList.forEach(builder::append);

        return builder.toString();
    }

    public StringLink append(String value){
        value = (value == null) ? "null" : value;
        linkList.add(value);

        return this;
    }

    public StringLink append(Object obj){
        obj = (obj == null) ? "null" : obj;
        return append(obj.toString());
    }

    public StringLink deleteLast(){
        if(!this.linkList.isEmpty()){
            int size = linkList.size();
            linkList.remove(size - 1);
        }
        return this;
    }

    public String getLast(){
        if (this.linkList.isEmpty())
            return "";
        else
            return this.linkList.get(this.linkList.size() - 1);
    }

    public boolean isEmpty(){
        return linkList.isEmpty();
    }

    public int getNodeCount(){
        return this.linkList.size();
    }

}
